package com.hu.network.http.request.impl;

import com.alibaba.fastjson.TypeReference;
import com.hu.network.http.bean.BaseRequestBean;
import com.hu.network.http.bean.InnerRequestBean;
import com.hu.network.http.request.BaseGetHttpRequest;
import com.hu.network.util.JsonHelper;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

import okhttp3.ResponseBody;

/**
 * 说明：
 *
 * @author ： hucanhua
 * @date ： 2017/10/25
 */
public class GetHttpRequestImpl extends BaseGetHttpRequest<String> {

    private final InnerRequestBean<BaseRequestBean> innerRequestBean;

    public GetHttpRequestImpl(InnerRequestBean<BaseRequestBean> innerRequestBean, final ProgressCallback requestCallback, final ProgressCallback responseCallback) {
        super(requestCallback, responseCallback);
        this.innerRequestBean = innerRequestBean;
    }

    @Override
    public Map<String, Object> getParams() throws IllegalAccessException {
        BaseRequestBean baseRequestBean = innerRequestBean.getParams();

        Map<String, Object> params = new HashMap<>();
        if (baseRequestBean != null) {
            Field[] declaredFields = baseRequestBean.getClass().getDeclaredFields();
            if (declaredFields != null) {
                for (Field declaredField : declaredFields) {
                    declaredField.setAccessible(true);
                    if (declaredField.getType().isAssignableFrom(String.class) || declaredField.getType().isAssignableFrom(File.class)) {
                        params.put(declaredField.getName(), declaredField.get(baseRequestBean));
                    }
                }
            }
        }
        return params;
    }

    @Override
    public String getUrl() {
        return innerRequestBean.getUrl();
    }

    @Override
    public String dealResponse(ResponseBody body) throws IOException {
        return body.string();
    }

    @Override
    public boolean useHttps() {
        return innerRequestBean.isHttps();
    }
}
